package com.es.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * 作用:
 * json工具类
 *
 * @author simon
 * @create 6/28/17 11:19 AM
 */
public class JsonUtils {

    /**
     * 对象转化为json格式字符串
     */
    public static String toJson(Object array) throws RuntimeException {
        return JSON.toJSONString(array);
    }

    /**
     * 对象转化为map结构
     */
    public static Map<String, Object> toMap(Object object) {
        JSONObject jsonObject = (JSONObject) JSON.toJSON(object);
        Set<Map.Entry<String, Object>> entrySet = jsonObject.entrySet();
        Map<String, Object> map = new TreeMap<>();
        for (Map.Entry<String, Object> entry : entrySet) {
            map.put(entry.getKey(), entry.getValue());
        }
        return map;
    }
}
